package com.gpj.learn.rabbitmq.pubsub;

import com.gpj.learn.rabbitmq.common.CommonConstants;
import com.gpj.learn.rabbitmq.common.RabbitUtil;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;

/**
 * @className PubSubProducer
 * @description
 *   TODO
 * @author GPJ
 * @date 2021/5/30 12:33
 * @version 1.0
 **/
@Slf4j
public class PubSubProducer {
	public static void main(String[] args) throws IOException {
		log.info("------------- 开始发送消息 -------------");
		Channel channel = RabbitUtil.createChannel();

		// 声明交换机
		channel.exchangeDeclare(
				CommonConstants.RABBITMQ_EXCHANGE_PUBSUB,
				BuiltinExchangeType.FANOUT,
				false, // 是否持久化
				false, // 是哦否自动删除
				false, // 是否内置,true-内置交换机,客户端线程无法直接发送消息到该交换机上
				null); // 额外信息

		String meaasge = "pubsub message no.";
		for (int i = 0; i < 5; i++) {
			// 向交换机发送消息,而不是直接向队列发送消息
			channel.basicPublish(CommonConstants.RABBITMQ_EXCHANGE_PUBSUB,
					"", null, (meaasge + i).getBytes());
		}
		RabbitUtil.close(channel);
		log.info("------------- 消息发送完成 -------------");
	}
}
